package com.bilibili.delayed;

import org.apache.rocketmq.client.consumer.DefaultMQPushConsumer;
import org.apache.rocketmq.client.consumer.listener.ConsumeConcurrentlyContext;
import org.apache.rocketmq.client.consumer.listener.ConsumeConcurrentlyStatus;
import org.apache.rocketmq.common.message.MessageExt;

import java.nio.charset.StandardCharsets;
import java.util.Date;
import java.util.List;

public class Consumer {
    public static void main(String[] args) throws Exception {
        //1.获取消费者对象
        DefaultMQPushConsumer consumer = new DefaultMQPushConsumer("delayed_consumer_group");

        //2.设置nameserver地址
        consumer.setNamesrvAddr("localhost:9876");

        //3.订阅Topic
        consumer.subscribe("delayed","hello");
        //4.注册信息监听
        consumer.registerMessageListener((List<MessageExt> list, ConsumeConcurrentlyContext consumeConcurrentlyContext)-> {
            try {
                for (MessageExt messageExt : list) {
                    System.out.println(new String(messageExt.getBody(), StandardCharsets.UTF_8));
                    System.out.println(new Date());
                }
                return ConsumeConcurrentlyStatus.CONSUME_SUCCESS;
            } catch (Exception e) {
                e.printStackTrace();
                return ConsumeConcurrentlyStatus.RECONSUME_LATER;
            }
        });
        //5.启动
        consumer.start();
    }
}
